<?php

namespace App\Http\Controllers\Admin\Speechcrafts;

use App\Http\Controllers\Controller;
use App\Models\StoreServiceSpeechcraft;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\Store\StoreServiceSpeechcraftTransformer;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class StoreServiceSpeechcraftsController extends Controller
{
    //客服话术的增删改查
    use AdminTraits;

    //定义模型和transformer
    public $model = StoreServiceSpeechcraft::class;
    public $transformer = StoreServiceSpeechcraftTransformer::class;


    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'PATCH':
            case 'PUT':
            case 'POST':
                return [
                    'title' => 'required|min:4|max:20',
                    'content' => 'required|min:10|max:255',
                    'nickname' => 'required|exists:categories',
                ];
                break;
        }
        return [

        ];
    }


    public function ruleMessages(): array
    {
        return [
            'title.required' => '话术标题 不能为空',
            'title.min' => '话术标题的字段 不能少于4个字段',
            'title.max' => '话术标题的字段 不能多于20个字段',
            'content.required' => '话术内容 不能为空',
            'content.min' => '话术内容的字段 不能少于10个字段',
            'content.max' => '话术内容的字段 不能多于255个字段',
            'nickname.required' => '话术分类 不能为空',
            'nickname.exists' => '话术分类 必须在这个分类表里面',
        ];
    }

    //列表
    public function index(){
        $data = $this->getQuery()
            ->pimp()
            ->with('categories:id,nickname')
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }
}
